<?php

class db{

/*** Declare instance ***/
private static $instance    = NULL;
private $registry;
private $connection;
public $result = array();

private $userManager;
private $configManager;
private $recordManager;
private $mapManager;
private $loginManager;
private $otherManager;
/**
*
* the constructor is set to private so
* so nobody can create a new instance using new
*
*/
public function __construct() {
  /*** maybe set the db name here later ***/
  $this->userManager    = new UserManager($this);
  $this->mapManager     = new MapManager($this);
  $this->configManager  = new ConfigManager($this);
  $this->recordManager  = new RecordManager($this);
  $this->otherManager   = new OtherManager($this);
  $this->registry       = new Registry($this);
}

/**
*
* Like the constructor, we make __clone private
* so nobody can clone the instance
*
*/

/**
 * start connection
 **/
public function connect(){
    $cfg = $this->configManager;
    
    $this->connection = mysql_connect($cfg->host, $cfg->username, $cfg->password);
    if(!$this->connection){
        echo 'Could not connect: '.mysql_error();
    }
    
    $selectdb = mysql_select_db($cfg->dbName, $this->connection);
    if(!$selectdb){
        echo 'Could not select db: '.mysql_error();
    }
    mysql_query("SET NAMES 'UTF8'");
}

public function disconnect(){
    if($this->connection){
        mysql_close($this->connection);
    }
}

function getManager($nameManager){
    return $this->$nameManager;
}

    /** Query database
     * @param $query
     * @return int
     * error    => -1
     * success  => numRows
     */
public function dbQuery($query){
    $result = mysql_query($query, $this->connection);
    if(!$result){
        echo 'Invalid query '.mysql_error().' '.$query;
        return -1;
    }
    $numRows = mysql_num_rows($result);
    $this->result = array("numRows" => $numRows, "content" => $result);
    return $numRows;
}

public function fetchResult(){
    if(isset($this->result['content'])){
        $row = mysql_fetch_assoc($this->result['content']);
        return $row; 
    }else{
        //TO-DO
        return false;
    }
}

public function fetchToJSON(){
    $result = array();
    if(isset($this->result['content'])){
        while($row = mysql_fetch_assoc($this->result['content'])){
             $result[] = $row;  
        }
        return json_encode($result);
    }else{
        //TO-DO
        return false;
    }
}


    public function getprovince(){
        $query = "SELECT pid, province FROM province";
        $result = mysql_query($query);
        $array = array();
        $array = 'var province = {'.'<br/>';
        while($row = mysql_fetch_assoc($result)){
            $query1 = "SELECT did, dname FROM district WHERE pid = ".$row['pid'] ;//echo $query1;
            $result1 = mysql_query($query1);

            $array .= '{"province" : "'.$row['province'].'", "value" : [';
            while($row1 = mysql_fetch_assoc($result1)) {
//                $array1  .=  $row1['district'];
                 $array.= '{"district": "'.$row1['district'].'"}, ';
            }
             $array .= ']},'.'<br/>';

        }
        $array .= '};';
        echo $array;
//        echo json_encode($array);
    }
} /*** end of class ***/


?>
